var $ = require('../../../utils/helper.js'),
  that = null
var app = getApp()
Page({
  data: {
    info: {
      type: 1,
    },
    cate_index: 0
  },
  onLoad(e) {
    that = this
  },
  onShow: function () {

  },
  onReady: function () {
    $.check(that)
  },
  init: function () {
    that.catelist()
  },
  changetype: function (e) {
    that.setData({
      ["info.type"]: e.detail.value
    })
  },
  setinput: function (e) {
    that.setData({
      ["info." + e.currentTarget.dataset.name]: e.detail.value
    })
  },
  settableinput: function (e) {
    that.setData({
      ["info.tablefield." + e.currentTarget.dataset.name]: e.detail.value
    })
    console.log(that.data.info.table)
  },
  upload: function (e) {
    console.log(e)
    wx.chooseImage({
      success(res) {
        let tempFilePaths = res.tempFilePaths
        $.upload("auth", tempFilePaths[0], function (data) {
          that.setData({
            ["info." + e.currentTarget.dataset.name]: data
          })
        })
      }
    })
  },
  save: function () {
    $.post("api/signup/edit", that.data.info, function () {
      console.log("提交成功")
    })
  },
  setmap: function () {
    wx.chooseLocation({
      success: function (res) {
        console.log(res)
        that.setData({
          ["info.latitude"]: res.latitude
        })
        that.setData({
          ["info.longitude"]: res.longitude
        })
        that.setData({
          ["info.address"]: res.address
        })
      }
    })
  },
  catelist: function () {
    $.post("api/signup/cate_lists", null, function (lists) {
      that.setData({
        cate_lists: lists
      })

      $.post("api/signup/gettableconfig", null, function (data) {
        if (data != null) {
          that.setData({
            info: data
          })
        }
      })
      that.setcate_core()
    })
  },
  setcate: function (e) {
    that.setData({
      cate_index: e.detail.value
    })
    that.setcate_core()

  },
  setcate_core: function () {
    that.setData({
      ['info.signup_cate_id']: that.data.cate_lists[that.data.cate_index].id
    })
    console.log(that.data.cate_lists)
  },
  settablebox: function (e) {
    console.log(e.detail.value)
    that.setData({ ["info.tablecheck"]: e.detail.value })
  },
  setconfig:function(e){
    $.post("api/signup/settableconfig",{config:e.detail.value},function(data){
      getApp().toast("编辑成功",2000,function(){
        wx.navigateBack({
          
        })
      })
    })
  }
})